package com.huawei.hms.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import com.huawei.hms.activity.a.b;
import com.huawei.hms.activity.a.c;
import com.huawei.hms.activity.a.d;
import com.huawei.hms.adapter.AvailableAdapter;
import com.huawei.hms.adapter.AvailableAdapter.AvailableCallBack;
import com.huawei.hms.common.internal.RequestHeader;
import com.huawei.hms.common.internal.ResponseHeader;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hms.support.hianalytics.HiAnalyticsUtil;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.JsonUtil;
import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class a implements IBridgeActivityDelegate {
    private static AtomicBoolean a = new AtomicBoolean(false);
    private RequestHeader b;
    private String c;
    private d d;
    private ResponseHeader e;
    private WeakReference<Activity> f;

    class a implements AvailableCallBack {
        final /* synthetic */ a a;

        private a(a aVar) {
            this.a = aVar;
        }

        public void onComplete(int i) {
            if (i == 0) {
                this.a.d();
                return;
            }
            HMSLog.i("ForegroundBusDelegate", "version check failed");
            this.a.a(0, "apk version is invalid");
        }
    }

    public void onBridgeActivityCreate(Activity activity) {
        this.f = new WeakReference(activity);
        Intent intent = activity.getIntent();
        String stringExtra = intent.getStringExtra("HMS_FOREGROUND_REQ_HEADER");
        this.b = new RequestHeader();
        if (this.b.fromJson(stringExtra)) {
            this.c = intent.getStringExtra("HMS_FOREGROUND_REQ_BODY");
            this.d = (d) intent.getSerializableExtra("HMS_FOREGROUND_REQ_INNER");
            if (this.d == null) {
                a(0, "inner header is invalid");
                return;
            } else if (TextUtils.isEmpty(this.b.getApiName())) {
                a(0, "action is invalid");
                return;
            } else {
                e();
                if (a.get()) {
                    a(0, "last request is processing");
                    return;
                } else {
                    c();
                    return;
                }
            }
        }
        a(0, "header is invalid");
    }

    public void onBridgeActivityDestroy() {
        g();
        this.f = null;
    }

    public boolean onBridgeActivityResult(int i, int i2, Intent intent) {
        if (i != 431057) {
            return false;
        }
        a.set(false);
        if (intent.hasExtra("HMS_FOREGROUND_RESP_HEADER")) {
            String stringExtra = intent.getStringExtra("HMS_FOREGROUND_RESP_HEADER");
            this.e = new ResponseHeader();
            JsonUtil.jsonToEntity(stringExtra, this.e);
        }
        h();
        com.huawei.hms.activity.a.a a = a(this.d.c());
        if (a == null) {
            a(i2, intent);
        } else {
            b a2 = a.a((Activity) this.f.get(), i2, intent);
            if (a2 == null) {
                a(i2, intent);
            } else {
                a(a2.b(), a2.a());
            }
        }
        return true;
    }

    public void onBridgeConfigurationChanged() {
    }

    public int getRequestCode() {
        return 431057;
    }

    public void onKeyUp(int i, KeyEvent keyEvent) {
    }

    private com.huawei.hms.activity.a.a a(String str) {
        return c.a().b(str);
    }

    private void a(int i, Intent intent) {
        HMSLog.i("ForegroundBusDelegate", "succeedReturn");
        Activity b = b();
        if (b != null) {
            b.setResult(i, intent);
            a();
        }
    }

    private void a(int i, String str) {
        HMSLog.e("ForegroundBusDelegate", str);
        Activity b = b();
        if (b != null) {
            com.huawei.hms.activity.a.a a = a(this.d.c());
            if (a != null) {
                b a2 = a.a((Activity) this.f.get(), i, str);
                if (a2 == null) {
                    b.setResult(0);
                } else {
                    b.setResult(a2.b(), a2.a());
                }
            } else {
                b.setResult(0);
            }
            a();
        }
    }

    private void a() {
        Activity b = b();
        if (b != null && !b.isFinishing()) {
            b.finish();
        }
    }

    private Activity b() {
        if (this.f == null) {
            return null;
        }
        return (Activity) this.f.get();
    }

    private void c() {
        AvailableCallBack aVar = new a();
        AvailableAdapter availableAdapter = new AvailableAdapter(this.d.a());
        int isHuaweiMobileServicesAvailable = availableAdapter.isHuaweiMobileServicesAvailable(b());
        if (isHuaweiMobileServicesAvailable == 0) {
            aVar.onComplete(isHuaweiMobileServicesAvailable);
        } else if (availableAdapter.isUserResolvableError(isHuaweiMobileServicesAvailable)) {
            a(b(), availableAdapter, aVar);
        } else {
            aVar.onComplete(isHuaweiMobileServicesAvailable);
        }
    }

    private static void a(Activity activity, AvailableAdapter availableAdapter, AvailableCallBack availableCallBack) {
        if (activity == null) {
            HMSLog.i("ForegroundBusDelegate", "null activity, could not start resolution intent");
        }
        availableAdapter.startResolution(activity, availableCallBack);
    }

    private void d() {
        HMSLog.i("ForegroundBusDelegate", "startApkHubActivity");
        Activity b = b();
        if (b == null) {
            HMSLog.e("ForegroundBusDelegate", "startApkHubActivity but activity is null");
            return;
        }
        String b2 = com.huawei.hms.utils.a.a(b.getApplicationContext()).b();
        Intent intent = new Intent(this.d.b());
        intent.putExtra("HMS_FOREGROUND_REQ_BODY", this.c);
        intent.setPackage(b2);
        intent.setClassName(b2, "com.huawei.hms.core.activity.UiJumpActivity");
        intent.putExtra("HMS_FOREGROUND_REQ_HEADER", this.b.toJson());
        intent.putExtra("intent.extra.hms.core.DELEGATE_NAME", "com.huawei.hms.core.activity.ForegroundBus");
        a.set(true);
        try {
            f();
            b.startActivityForResult(intent, 431057);
        } catch (Throwable e) {
            a.set(false);
            HMSLog.e("ForegroundBusDelegate", "Launch sign in Intent failed. hms is probably being updated：", e);
            a(0, "launch bus intent failed");
        }
    }

    private void e() {
        Map mapFromForegroundRequestHeader = HiAnalyticsUtil.getInstance().getMapFromForegroundRequestHeader(this.b);
        mapFromForegroundRequestHeader.put("direction", "req");
        mapFromForegroundRequestHeader.put("version", String.valueOf(this.b.getKitSdkVersion()));
        if (b() != null) {
            HiAnalyticsUtil.getInstance().onNewEvent(b().getApplicationContext(), HiAnalyticsConstant.HMS_SDK_BASE_ACTIVITY_STARTED, mapFromForegroundRequestHeader);
        }
    }

    private void f() {
        Map mapFromForegroundRequestHeader = HiAnalyticsUtil.getInstance().getMapFromForegroundRequestHeader(this.b);
        mapFromForegroundRequestHeader.put("direction", "req");
        mapFromForegroundRequestHeader.put("version", String.valueOf(this.b.getKitSdkVersion()));
        if (b() != null) {
            HiAnalyticsUtil.getInstance().onNewEvent(b().getApplicationContext(), HiAnalyticsConstant.HMS_SDK_BASE_START_CORE_ACTIVITY, mapFromForegroundRequestHeader);
        }
    }

    private void g() {
        if (this.b != null) {
            Map mapFromForegroundRequestHeader = HiAnalyticsUtil.getInstance().getMapFromForegroundRequestHeader(this.b);
            mapFromForegroundRequestHeader.put("direction", "rsp");
            mapFromForegroundRequestHeader.put("version", String.valueOf(this.b.getKitSdkVersion()));
            if (this.e != null) {
                mapFromForegroundRequestHeader.put("statusCode", String.valueOf(this.e.getStatusCode()));
                mapFromForegroundRequestHeader.put(HiAnalyticsConstant.BI_KEY_RESUST, String.valueOf(this.e.getErrorCode()));
            }
            if (b() != null) {
                HiAnalyticsUtil.getInstance().onNewEvent(b().getApplicationContext(), HiAnalyticsConstant.HMS_SDK_BASE_ACTIVITY_STARTED, mapFromForegroundRequestHeader);
            }
        }
    }

    private void h() {
        Map mapFromForegroundRequestHeader = HiAnalyticsUtil.getInstance().getMapFromForegroundRequestHeader(this.b);
        mapFromForegroundRequestHeader.put("direction", "rsp");
        mapFromForegroundRequestHeader.put("version", String.valueOf(this.b.getKitSdkVersion()));
        if (this.e != null) {
            mapFromForegroundRequestHeader.put("statusCode", String.valueOf(this.e.getStatusCode()));
            mapFromForegroundRequestHeader.put(HiAnalyticsConstant.BI_KEY_RESUST, String.valueOf(this.e.getErrorCode()));
        }
        if (b() != null) {
            HiAnalyticsUtil.getInstance().onNewEvent(b().getApplicationContext(), HiAnalyticsConstant.HMS_SDK_BASE_START_CORE_ACTIVITY, mapFromForegroundRequestHeader);
        }
    }
}
